package com.mini.cn.goods.io.netty.ws;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * Created with IntelliJ IDEA.

 * User: fly
 * Date: 2020-01-16
 * Time: 7:19 下午
 */
public final class WebSocketChannelInitializer extends ChannelInitializer<NioSocketChannel> {

    private final HttpUpgradeHandler httpServerHandler;

    public WebSocketChannelInitializer(HttpUpgradeHandler inboundHandlerAdapter) {
        this.httpServerHandler = inboundHandlerAdapter;
    }

    @Override
    protected void initChannel(NioSocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(new ChunkedWriteHandler());
        pipeline.addLast(new HttpObjectAggregator(65536));
        pipeline.addLast(httpServerHandler);
    }
}
